package com.samehope.ar.controller;


import com.samehope.ar.common.CommonResult;
import com.samehope.ar.dto.PointGroupAddParam;
import com.samehope.ar.dto.PointGroupUpdateParam;
import com.samehope.ar.service.IPatrolPointGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 巡检点分组表 前端控制器
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/pointGroup")
public class PatrolPointGroupController {

    @Autowired
    private IPatrolPointGroupService pointGroupService;

    /**
     * 新增点位分组
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PostMapping("/add")
    public CommonResult add(@Valid @RequestBody PointGroupAddParam param) {
        pointGroupService.add(param);
        return CommonResult.success(null);
    }

    /**
     * 更新点位分组信息
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PutMapping("/update")
    public CommonResult update(@Valid @RequestBody PointGroupUpdateParam param) {
        pointGroupService.update(param);
        return CommonResult.success(null);
    }

    /**
     * 删除点位分组
     * @param groupId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @DeleteMapping("/delete/{groupId}")
    public CommonResult delete(@PathVariable("groupId") Long groupId) {
        pointGroupService.delete(groupId);
        return CommonResult.success(null);
    }

    /**
     * 查询巡检点分组树结构
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/tree")
    public CommonResult tree(@RequestParam(required = false) String condition) {
        return CommonResult.success(pointGroupService.getGroupTree(condition));
    }

    /**
     * 巡检点分组下拉框视图对象
     * @return
     */
    @GetMapping("/groupsForSelect")
    public CommonResult groupsForSelect() {
        return CommonResult.success(pointGroupService.groupsForSelect());
    }
}
